/*
 * @Author: taoyuqing
 * @Date: 2023-03-04 05:47:10
 * @Description: 
 */
import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/material.dart';

import '../../utils/utils.dart';

class RulesBanner extends StatefulWidget {
  final List<Widget> imageList;
  final double? height;
  final int index;
  final Function? onchange;
  RulesBanner(
      {Key? key,
      required this.imageList,
      this.height,
      required this.index,
      this.onchange})
      : super(key: key);

  @override
  State<RulesBanner> createState() => RulesBannerState();
}

class RulesBannerState extends State<RulesBanner> {
  CarouselController _carouselController = CarouselController();
  // final CarouselController _controller = CarouselController();
  @override
  Widget build(BuildContext context) {
    final double WIDTH = Utils().getScreenWidth(context) - 40;
    return Column(
      children: [
        CarouselSlider(
          carouselController: _carouselController,
          options: CarouselOptions(
              height: widget.height ?? 400,
              autoPlay: false,
              viewportFraction: 1,
              onPageChanged: (index, reason) => {
                    setState(() {
                      if (widget.onchange != null) {
                        widget.onchange!(index);
                      }
                      // (index);
                    })
                  }),
          items: widget.imageList.map((i) {
            return Builder(
              builder: (BuildContext context) {
                return i;
              },
            );
          }).toList(),
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: widget.imageList.asMap().keys.map((entry) {
            return GestureDetector(
              onTap: () {},
              child: Container(
                width: widget.index == entry ? 24 : 8.0,
                height: 8.0,
                margin:
                    const EdgeInsets.symmetric(vertical: 8.0, horizontal: 4.0),
                decoration: BoxDecoration(
                    borderRadius: const BorderRadius.all(Radius.circular(4.0)),
                    color: widget.index == entry
                        ? Colors.yellow
                        : const Color(0x33ffffff)),
              ),
            );
          }).toList(),
        )
      ],
    );
  }
  jumpToPage(int page){
    _carouselController.animateToPage(page);
  }
}
